package com.aw.baselib.widgets.RoundImageView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.aw.baselib.util.AwDisplayUtil;

public class AwCircleView extends View {

    private Context mContext;
    private Paint paint; // 画笔对象
    private String colorStr = "#64E155";

    public AwCircleView (Context context) {
        super(context);
        mContext = context;
        init();
    }

    public AwCircleView (Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public AwCircleView (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true); // 开启抗锯齿功能
    }

    public void setColor(String colorStr) {
        this.colorStr = colorStr;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(AwDisplayUtil.dip2px(mContext, 44), AwDisplayUtil.dip2px(mContext, 44)); // 设置视图大小为正方形
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = getWidth() / 2; // 获取视图中心点 x 坐标
        int centerY = getHeight() / 2; // 获取视图中心点 y 坐标
        float radius = Math.min(centerX, centerY) - 10; // 计算半径值

        RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius); // 构造矩形区域

        paint.setColor(Color.parseColor(colorStr));
//        // 根据需要设置不同颜色的圆形背景
//        if (isSelected()) {
//            paint.setColor(Ciki==); // 选中状态时的颜色
//        } else {
//            paint.setColor(getResources().getColor(R.color.normal_circle)); // 非选中状态时的颜色
//        }

        canvas.drawArc(rectF, 0, 360, true, paint); // 绘制弧线
    }
}
